﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace PMS.App
{
    enum Status
    {
        New,
        Update
    }

    public partial class ucDepartment : UserControl
    {
        public ucDepartment()
        {
            InitializeComponent();
          
        }
        Status status = Status.Update;
        private void ucRoom_Load(object sender, EventArgs e)
        {
            this.phongBanTableAdapter.Fill(dsPMS.PhongBan);
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            if (status == Status.Update)
            {
                if (XtraMessageBox.Show("Do you want to save changes?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    this.phongBanTableAdapter.Update(this.dsPMS.PhongBan);
                }
                else
                {
                    this.phongBanTableAdapter.Fill(this.dsPMS.PhongBan);
                }
            }
            if (status == Status.New)
            {
                this.phongBanTableAdapter.Update(this.dsPMS.PhongBan);
                status = Status.Update;
            }
           
        }

        private void datnaviMain_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {

            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.EndEdit:
                    if (XtraMessageBox.Show("Do you want to save changes?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        this.phongBanTableAdapter.Update(this.dsPMS.PhongBan);
                    }
                    else
                    {
                        this.phongBanTableAdapter.Fill(this.dsPMS.PhongBan);
                    }
                    break;
                case NavigatorButtonType.CancelEdit:
                    if (XtraMessageBox.Show("Are you sure you want to delete a Department?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        dsPMS.PhongBanRow phongBan = ((DataRowView)this.phongBanBindingSource.Current).Row as dsPMS.PhongBanRow;
                        dsPMSTableAdapters.NhanVienTableAdapter nhanVienAdapter = new dsPMSTableAdapters.NhanVienTableAdapter();
                        dsPMS.NhanVienDataTable nhanVienDataTable = new App.dsPMS.NhanVienDataTable();
                        nhanVienAdapter.Fill(nhanVienDataTable);
                        int count = (from nv in nhanVienDataTable.AsEnumerable() where nv.PhongBan_ID == phongBan.PhongBan_ID select nv).Count();
                        if (count > 0)
                        {
                            XtraMessageBox.Show("Can't Delete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            this.phongBanBindingSource.Remove(this.phongBanBindingSource.Current);
                            this.phongBanTableAdapter.Update(this.dsPMS.PhongBan);
                        }
                    }
                    break;
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (XtraMessageBox.Show("Are you sure you want to delete a Department?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    dsPMS.PhongBanRow phongBan = ((DataRowView)this.phongBanBindingSource.Current).Row as dsPMS.PhongBanRow;
                    dsPMSTableAdapters.NhanVienTableAdapter nhanVienAdapter = new dsPMSTableAdapters.NhanVienTableAdapter();
                    dsPMS.NhanVienDataTable nhanVienDataTable = new App.dsPMS.NhanVienDataTable();
                    nhanVienAdapter.Fill(nhanVienDataTable);
                    int count = (from nv in nhanVienDataTable.AsEnumerable() where nv.PhongBan_ID == phongBan.PhongBan_ID select nv).Count();
                    if (0 < count)
                    {
                        XtraMessageBox.Show("Can't Delete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        this.phongBanBindingSource.Remove(this.phongBanBindingSource.Current);
                        this.phongBanTableAdapter.Update(this.dsPMS.PhongBan);
                    }
                }
            }
        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            status = Status.New;
        } 
    }
}
